package com.hxd.mall.utils.open

import android.content.Context
import android.net.ConnectivityManager
import android.net.ConnectivityManager.TYPE_MOBILE
import android.net.ConnectivityManager.TYPE_WIFI
import android.net.wifi.WifiManager
import com.hxd.mall.constant.HxdConstantsNet.Risk_Net_Type_Mobile
import com.hxd.mall.constant.HxdConstantsNet.Risk_Net_Type_No
import com.hxd.mall.constant.HxdConstantsNet.Risk_Net_Type_Wifi
import com.hxd.mall.constant.HxdGlobalInfo
import com.hxd.mall.constant.HxdGlobalInfo.instance
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.NetworkInterface.getNetworkInterfaces
import java.net.SocketException

/**
 * Created by qishuai on 2018/8/13.
 */
object DeviceInfoUtil {
    /**
     * 获取ip地址
     */
    fun getIPAddress(): String {
        val info = (HxdGlobalInfo.instance
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
        if (info != null && info.isConnected) {
            if (info.type == TYPE_MOBILE) {//当前使用2G/3G/4G网络
                try {
                    val en = NetworkInterface.getNetworkInterfaces()
                    while (en.hasMoreElements()) {
                        val intf = en.nextElement()
                        val enumIpAddr = intf.inetAddresses
                        while (enumIpAddr.hasMoreElements()) {
                            val inetAddress = enumIpAddr.nextElement()
                            if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                                return inetAddress.getHostAddress()
                            }
                        }
                    }
                } catch (e: SocketException) {
                    e.printStackTrace()
                }

            } else if (info.type == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                val wifiManager = HxdGlobalInfo.instance.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
                val wifiInfo = wifiManager.connectionInfo
                return intIP2StringIP(wifiInfo.ipAddress)
            }
        }

        return "127.0.0.0"
    }

    /**
     * 将得到的int类型的IP转换为String类型
     *
     * @param ip
     * @return
     */
    private fun intIP2StringIP(ip: Int): String {
        return (ip and 0xFF).toString() + "." +
                (ip shr 8 and 0xFF) + "." +
                (ip shr 16 and 0xFF) + "." +
                (ip shr 24 and 0xFF)
    }

    /**
     * 获取Mac地址
     * @param context
     * @return
     */
    fun getMacAddressFromIp(context: Context): String {
        var macS = ""
        val buf = StringBuilder()
        try {
            val mac: ByteArray
            val ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getIPAddress()))
            mac = ne.hardwareAddress
            for (b in mac) {
                buf.append(String.format("%02X:", b))
            }
            if (buf.isNotEmpty()) {
                buf.deleteCharAt(buf.length - 1)
            }
            macS = buf.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return macS
    }

    /**
     * 获取网络类型
     */
    fun getNetType(): Int {
        val info = (instance.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).activeNetworkInfo
        if (info != null && info.isConnected) {
            when (info.type) {
                //当前使用2G/3G/4G网络
                TYPE_MOBILE -> {
                    try {
                        val en = getNetworkInterfaces()
                        while (en.hasMoreElements()) {
                            val intf = en.nextElement()
                            val enumIpAddr = intf.inetAddresses
                            while (enumIpAddr.hasMoreElements()) {
                                val inetAddress = enumIpAddr.nextElement()
                                if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                                    return Risk_Net_Type_Mobile
                                }
                            }
                        }
                    } catch (e: SocketException) {
                        e.printStackTrace()
                    }
                }
                //当前使用无线网络
                TYPE_WIFI -> return Risk_Net_Type_Wifi
            }
        } else {
            //当前无网络连接,请在设置中打开网络
        }
        return Risk_Net_Type_No
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return  系统版本号
     */
    fun getSystemVersion(): String {
        return android.os.Build.VERSION.RELEASE
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    fun getSystemModel(): String {
        return android.os.Build.MODEL
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    fun getDeviceBrand(): String {
        return android.os.Build.BRAND
    }
}